package com.financing.platform.activities.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.financing.platform.activities.ActivitiesBusinessObject;
import com.financing.platform.dao.IActivitiesMollionsWealthDao;
import com.financing.platform.dao.IActivitiesWinListDao;
import com.financing.platform.dao.IOrderDao;
import com.financing.platform.dto.ActivitesMollionsWealthDTO;
import com.financing.platform.dto.ActivitiesWinListDTO;
import com.financing.platform.dto.BaseDTO;
import com.financing.platform.dto.FaUserDTO;
import com.financing.platform.dto.OrderDTO;
import com.financing.platform.dto.ProjectDTO;
import com.financing.platform.service.IActivitesService;
import com.financing.platform.util.DateUtil;
import com.financing.platform.vo.OrderActivitiesVO;

/**
 * 百万冲十亿 活动
 * @author lisu
 *
 */
@Service("activitiesBWCSWImpl")
public class ActivitiesBWCSWImpl extends ActivitiesBusinessObject {

	protected Logger log = Logger.getLogger(this.getClass());
	private final String ACTIVIESCODE = "BWCSY";
	@Autowired
	private IActivitiesWinListDao activitiesWinListDaoImpl;
	
	@Autowired
	private IOrderDao orderDaoImpl;
	
	@Autowired
	private IActivitesService activitesServiceImpl;
	@Autowired
	private IActivitiesMollionsWealthDao activitiesMollionsWealthDaoImpl;
	/**
	 * 百万冲十亿活动-认证
	 * @throws Exception 
	 *
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void handleFaUserAuth(Object object) throws Exception {
		HashMap<String, Object> map = (HashMap<String, Object>) object;
		FaUserDTO faUserDTO = (FaUserDTO) map.get("faUserDTO");
		int updateInt = (Integer) map.get("updateInt");

		// 表示 修改成功 且是认证通过
		if (updateInt > 0) {
			ActivitiesWinListDTO activitiesWinListDTO = new ActivitiesWinListDTO();
			activitiesWinListDTO.setUserId(faUserDTO.getId());
			activitiesWinListDTO.setActiviesCode(ACTIVIESCODE);
			activitiesWinListDTO.setWinType("01");
			if(!"1".equals(faUserDTO.getuRole())
					&& !"99".equals(faUserDTO.getuRole())
					&& !"98".equals(faUserDTO.getuRole())){
				//删除  中奖表的认证数据
				
				activitiesWinListDTO.setCustomParameters("fawl.WIN_TYPE = '01'");
				activitiesWinListDTO.setUpdateBy(faUserDTO.getUpdateBy());
				activitiesWinListDTO.setUpdateDate(faUserDTO.getUpdateDate());
				activitiesWinListDaoImpl.delActivitiesWinList(activitiesWinListDTO);
				log.info("delActivitiesWinList Success ");
				//添加  中奖表
				activitiesWinListDTO.setCreateBy(faUserDTO.getUpdateBy());
				activitiesWinListDTO.setCreateDate(faUserDTO.getUpdateDate());
				activitiesWinListDTO.setWinDate(faUserDTO.getUpdateDate());
				activitiesWinListDaoImpl.addActivitiesWinList(activitiesWinListDTO);
				log.info("addActivitiesWinList Success ");
			}
			if("99".equals(faUserDTO.getuRole())){
				//删除  中奖几率表的认证数据
				activitiesWinListDTO.setCustomParameters("fawl.WIN_TYPE = '01'");
				activitiesWinListDTO.setUpdateBy(faUserDTO.getUpdateBy());
				activitiesWinListDTO.setUpdateDate(faUserDTO.getUpdateDate());
				activitiesWinListDaoImpl.delActivitiesWinList(activitiesWinListDTO);
				log.info("delActivitiesWinList Success 99");
			}

		}
	}
	/**
	 * 百万冲十亿活动-产品成立
	 * @throws Exception 
	 *
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void handleProductFound(Object object) throws Exception {
		HashMap<String, Object> map = (HashMap<String, Object>) object;
		ProjectDTO projectDTO = (ProjectDTO) map.get("projectDTO");
		int updateInt = (Integer) map.get("updateInt");

		// 表示 修改成功 
		if (updateInt > 0) {
			
			log.info("handleProductFound begin");
			
			OrderDTO orderDTO = new OrderDTO();
			orderDTO.setoProjectId(projectDTO.getId());
			
			ActivitesMollionsWealthDTO activitiesMollionsWealthDTO = new ActivitesMollionsWealthDTO();
			activitiesMollionsWealthDTO.setpId(projectDTO.getId());
			activitiesMollionsWealthDTO.setActivitiesCode(ACTIVIESCODE);
			//查询当前订单产品是否有活动
			List<ActivitesMollionsWealthDTO> listActivitesMollionsWealth = activitiesMollionsWealthDaoImpl.queryActivitiesMollionsWealthProduct(activitiesMollionsWealthDTO);
			
			if(listActivitesMollionsWealth!=null && listActivitesMollionsWealth.size() == 1){
				//获取产品活动类型  活动产品类型 bwcf(01：新手奖励 02：认证奖励 03:不限)bwcsy（04：主推 05：非主推）
				ActivitesMollionsWealthDTO activitesMollionsWealthDTO = listActivitesMollionsWealth.get(0);
				String activitiesProductType = activitesMollionsWealthDTO!=null?activitesMollionsWealthDTO.getActivitiesProductType():"";
				log.info("handleProductFound Activites ing");
				//获取 产品成立，拥有订单的用户者
				List<String> list = orderDaoImpl.queryNeedSendSMSFAId(orderDTO);
				
				for (int i = 0; i < list.size(); i++) {
					String userId= list.get(i);
					if(userId == null){
						continue;
					}
					//计算用户中奖记录
					calculationUserWinList(userId,projectDTO,activitiesProductType);
				}
			}
			log.info("handleProductFound end");
		}
	}
	/** 
	 * 百万冲十亿-订单拉黑
	 * @param object
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void handleOrderDefriend(Object object) throws Exception{
		HashMap<String,Object> map =(HashMap<String, Object>)object;
		OrderDTO orderDTO = (OrderDTO)map.get("orderDTO");
		int updateInt = (Integer) map.get("updateInt");

		// 表示 修改成功 
		if (updateInt > 0) {
			OrderDTO  bean  = new  OrderDTO();
			bean.setId(orderDTO.getId());
			bean = orderDaoImpl.queryOrder(bean);
			if(bean==null){
				return ;
			}
			ActivitesMollionsWealthDTO activitiesMollionsWealthDTO = new ActivitesMollionsWealthDTO();
			activitiesMollionsWealthDTO.setpId(bean.getoProjectId());
			activitiesMollionsWealthDTO.setActivitiesCode(ACTIVIESCODE);
			//查询当前订单产品是否有活动
			List<ActivitesMollionsWealthDTO> listActivitesMollionsWealth = activitiesMollionsWealthDaoImpl.queryActivitiesMollionsWealthProduct(activitiesMollionsWealthDTO);
			//查询当前订单产品是否有活动   且预约时间在活动时间内
			if(listActivitesMollionsWealth!=null && listActivitesMollionsWealth.size() == 1 && !activitesServiceImpl.isOutDateByCode(ACTIVIESCODE, bean.getCreateDate())){
				log.info("handleOrderDefriend calculationUserWinList");
				//获取产品活动类型  活动产品类型 bwcf(01：新手奖励 02：认证奖励 03:不限)bwcsy（04：主推 05：非主推）
				ActivitesMollionsWealthDTO activitesMollionsWealthDTO = listActivitesMollionsWealth.get(0);
				String activitiesProductType = activitesMollionsWealthDTO!=null?activitesMollionsWealthDTO.getActivitiesProductType():"";
				//计算用户中奖记录
				calculationUserWinList(bean.getoFAId(),orderDTO,activitiesProductType);
			}
		}
	}
	/**
	 * 计算用户中奖记录
	 * @param userId
	 * @param object
	 * @throws SQLException 
	 * @throws Exception
	 */
	private void calculationUserWinList(String userId,BaseDTO object,String activitiesProductType) throws SQLException {
		List<OrderActivitiesVO> listMap = orderDaoImpl.queryOrderForUserId(userId);
		//订单累计金额
		BigDecimal oAmount= new BigDecimal(0);
		//首单预约时间
		Date appDateFirst = null;
		//首单成立时间
		Date setUpDateFirst = null;
		//首单订单金额
		BigDecimal oAmountFirst= null;
		//首单产品活动类型
		String activitiesProductTypeFirst = null;
		
		Boolean iphoneFlag = true;
		Boolean macbookAirFlag = true;
		
		//先删除中奖记录
		ActivitiesWinListDTO activitiesWinListDTO = new ActivitiesWinListDTO();
		activitiesWinListDTO.setUserId(userId);
		activitiesWinListDTO.setActiviesCode(ACTIVIESCODE);
		activitiesWinListDTO.setCreateBy(object.getUpdateBy());
		activitiesWinListDTO.setCreateDate(object.getUpdateDate());
		activitiesWinListDTO.setUpdateBy(object.getUpdateBy());
		activitiesWinListDTO.setUpdateDate(object.getUpdateDate());
		activitiesWinListDTO.setCustomParameters("fawl.WIN_TYPE != '01'");
		activitiesWinListDaoImpl.delActivitiesWinList(activitiesWinListDTO);
		
		if(listMap.size() <=0){
			return;
		}
		log.info("handleProductFound delActivitiesWinList  Success ");
		for (int j = 0; j < listMap.size(); j++) {
			OrderActivitiesVO mapData = listMap.get(j);
			//获取首次订单
			if(j==0){
				activitiesProductTypeFirst = mapData.getActivitiesProductType();
				appDateFirst = mapData.getAppDate();
				setUpDateFirst = mapData.getSetUpDate();
				oAmountFirst = mapData.getoAmount();
			}
			
			//预约时间
			Date appDate = mapData.getAppDate();
			//成立时间
			Date setUpDate = mapData.getSetUpDate();
			
			//预约时间和成立时间  是否在活动期间
			if(activitesServiceImpl.isOutDateByCode(ACTIVIESCODE, appDate) ){
				continue;
			}
			oAmount = oAmount.add( mapData.getoAmount());

			
			activitiesWinListDTO.setWinDate(setUpDate);
			

			activitiesWinListDTO.setWinDate(setUpDate);
			if(oAmount.compareTo(new BigDecimal(10000000))>=0 && iphoneFlag){
				activitiesWinListDTO.setWinType("04");
				activitiesWinListDaoImpl.addActivitiesWinList(activitiesWinListDTO);
				iphoneFlag= false;
				log.info("handleProductFound addActivitiesWinList  Success 04");
			}
			if(oAmount.compareTo(new BigDecimal(20000000))>=0 && macbookAirFlag){
				activitiesWinListDTO.setWinType("05");
				activitiesWinListDaoImpl.addActivitiesWinList(activitiesWinListDTO);
				macbookAirFlag= false;
				log.info("handleProductFound addActivitiesWinList  Success 05");
			}
			
		}
		log.info("handleProductFound appDateFirst:"+DateUtil.getString(appDateFirst,DateUtil.DEFAULT_DATE_FORMAT));
		log.info("handleProductFound setUpDateFirst:"+DateUtil.getString(setUpDateFirst,DateUtil.DEFAULT_DATE_FORMAT));
		log.info("handleProductFound oAmountFirst:"+oAmountFirst);
		//判断是否是首单
		if(!activitesServiceImpl.isOutDateByCode(ACTIVIESCODE, appDateFirst) ){
			activitiesWinListDTO.setWinDate(setUpDateFirst);
			if("04".equals(activitiesProductTypeFirst)){
				if(oAmountFirst.compareTo(new BigDecimal(1000000))>=0 && oAmountFirst.compareTo(new BigDecimal(3000000))==-1){
					activitiesWinListDTO.setWinType("06");
					activitiesWinListDaoImpl.addActivitiesWinList(activitiesWinListDTO);
					log.info("handleProductFound addActivitiesWinList  Success 06");
				}else if(oAmountFirst.compareTo(new BigDecimal(3000000))>=0){
					activitiesWinListDTO.setWinType("07");
					activitiesWinListDaoImpl.addActivitiesWinList(activitiesWinListDTO);
					log.info("handleProductFound addActivitiesWinList  Success 07");
				}
			}else if("05".equals(activitiesProductTypeFirst)){
				if(oAmountFirst.compareTo(new BigDecimal(1000000))>=0 && oAmountFirst.compareTo(new BigDecimal(3000000))==-1){
					activitiesWinListDTO.setWinType("08");
					activitiesWinListDaoImpl.addActivitiesWinList(activitiesWinListDTO);
					log.info("handleProductFound addActivitiesWinList  Success 08");
				}else if(oAmountFirst.compareTo(new BigDecimal(3000000))>=0){
					activitiesWinListDTO.setWinType("09");
					activitiesWinListDaoImpl.addActivitiesWinList(activitiesWinListDTO);
					log.info("handleProductFound addActivitiesWinList  Success 09");
				}
			}
			
		}
	}
}
